package com.ruoyi.other.api;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.HmacSha;
import com.ruoyi.common.utils.ProjectConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.other.service.AppOtherService;

import io.swagger.annotations.Api;


@RestController
@RequestMapping("/api/v1/agora")
@Api(tags = "【APP】声网回调接收Controller")
@CrossOrigin
public class AppAgoraController {

	@Autowired
	private AppOtherService appOtherService;
	
	
	
	/**
	 * 	声网回调
	 * @param payload
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/notifyAgora", method = { RequestMethod.POST, RequestMethod.GET })
	public AjaxResult notifyAgora(@RequestBody String payload,HttpServletRequest request) {
		String agoraSignature = request.getHeader("Agora-Signature");
		String agoraSignatureV2 = request.getHeader("Agora-Signature-V2");
		System.out.println("头部：");
		System.out.println("Agora-Signature:"+agoraSignature);
		System.out.println("Agora-Signature-V2:"+agoraSignatureV2);
		System.out.println("声网回调：");
		System.out.println(payload);
		// 验证回调是否正常
		if(StringUtils.isEmpty(agoraSignature) || StringUtils.isEmpty(agoraSignatureV2)) {
			return AjaxResult.error();
		}
		if(!agoraSignature.equals(HmacSha.hmacSha1(payload, ProjectConstant.RTC_CHANNEL_EVENT_CALLBACKS_SECRET))
				|| !agoraSignatureV2.equals(HmacSha.hmacSha256(payload, ProjectConstant.RTC_CHANNEL_EVENT_CALLBACKS_SECRET))) {
			return AjaxResult.error();
		}
		// 数据处理
		return appOtherService.notifyAgora(payload);
	}
}
